package com.lysj.food.food.service;

import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.food.food.domain.DefaultFood;
import com.lysj.food.food.domain.Food;
import com.lysj.food.food.dto.FoodAndCount;
import com.lysj.food.food.repository.DefaultFoodRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by wtl on 2019-03-25 14:27:56
 * @description 默认下单菜品服务层
 */
@Slf4j
@Service
@Transactional
public class DefaultFoodService implements BaseService<DefaultFood> {

    @Resource
    private DefaultFoodRepository defaultFoodRepository;

    @Override
    public DefaultFoodRepository getRepository() {
        return defaultFoodRepository;
    }

    @Resource
    private FoodService foodService;

    /**
     * @author Created by wtl on 2019/3/26 17:57
     * @Description 根据门店id获取对应的默认下单菜品列表
     * @param storeId 门店id
     */
    public List<DefaultFood> findByStoreId(String storeId) {
        List<DefaultFood> defaultFoods = defaultFoodRepository.findByStoreId(storeId);
        // 处理结果
        for (DefaultFood defaultFood : defaultFoods) {
            Food food = foodService.findOne(defaultFood.getFoodId());
            defaultFood.setFoodName(food.getName());
            // TODO:
//            defaultFood.setGuiGe(food.getGuiGeIds());
//            defaultFood.setSellFee(food.getSellFee());
        }
        return defaultFoods;
    }

    /**
     * @author Created by wtl on 2019/3/27 17:20
     * @Description 批量添加默认菜品
     */
    public String add(String storeId, List<FoodAndCount> foodAndCounts) {
        getRepository().deleteAll();
        List<DefaultFood> defaultFoods = new ArrayList<>();
        if (!ParamUtil.isBlank(foodAndCounts)) {
            for (FoodAndCount foodAndCount : foodAndCounts) {
                DefaultFood defaultFood = new DefaultFood();
                defaultFood.setStoreId(storeId);
                defaultFood.setFoodId(foodAndCount.getFoodId());
                defaultFood.setCount(foodAndCount.getCount());
                defaultFoods.add(defaultFood);
            }
            save(defaultFoods);
        }
        return "添加成功";
    }

}